package com.jsmtr.rms.controller.DUserResume;

import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/2/27 15:07
 * @modified by:
 */
@Controller
public class DUserResumeFlowController {
    @Resource
    private IProjectService projectService;

    //region  [start] 获得招聘实施流程信息
    @RequestMapping(value = "/**/getFlowsJson",produces = "application/json;charset=utf-8")
    public @ResponseBody
    String getFlowsJson(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);
            StringBuilder result = new StringBuilder();
            String upid=pid.toUpperCase();
            String ptype=null;
            if(upid.indexOf("A_")>=0){
                ptype="SZ";
            }else if((upid.indexOf("B_")>=0)||(upid.indexOf("F_")>=0)){
                ptype="GX";
            }else if(upid.indexOf("C_")>=0){
                ptype="ZZ";
            }else if(upid.indexOf("D_")>=0){
                ptype="NZ";
            }else if(upid.indexOf("E_")>=0){
                ptype="ZZCJ";
            }
            if (ptype.equalsIgnoreCase("ZZ")){
                result=createZZFlowJson(flows,ptype,request);
            }else{
                result=createNomalFlowJson(flows,ptype,request);
            }
            //utilService.printTextResult(result.toString(), response);
            return result.toString();
        } catch (Exception e) {
            //utilService.printTextResult("", response);
            return "";
        }
    }
    //endregion

    public StringBuilder createNomalFlowJson(List<ProjectInfoFlow> flowList,String projectType,HttpServletRequest request){
        String pid = request.getParameter("pid").trim();
        String deptcode = request.getParameter("deptcode").trim();
        StringBuilder flowsJson=new StringBuilder();
        flowsJson.append("[");
        for (ProjectInfoFlow flow : flowList) {
            int ftype = flow.getFid();
            String flowname=flow.getFname();
            if(!StringTools.trimNotEmpty(flowname)){
                flowname=flow.getNickname();
            }
            String url = null;

            if (flowname.equalsIgnoreCase("简历筛选")) {
                url = projectType+"Resume/";
            } else if (flowname.equalsIgnoreCase("笔试")) {
                url = projectType+"Exam/";
            }else if (flowname.equalsIgnoreCase("资料审核")) {
                url = projectType+"Check/";
            } else if (flowname.equalsIgnoreCase("面试")) {
                url = projectType+"Interview/";
            } else if (flowname.equalsIgnoreCase("背景调查")||flowname.equalsIgnoreCase("政审")) {
                url = projectType+"PReview/";
            } else if (flowname.equalsIgnoreCase("体检")) {
                url = projectType+"Physical/";
            }else if (flowname.equalsIgnoreCase("实作")) {
                url = projectType+"Operation/";
            }else if (flowname.equalsIgnoreCase("实习")) {
                url = projectType+"Practice/";
            }else if (flowname.equalsIgnoreCase("签约")) {
                url = projectType+"Signing/";
            }else if (flowname.equalsIgnoreCase("录用")) {
                url = projectType+"Employ/";
            } else {
                url = "";
            }
            String href=request.getContextPath() + "/Do/" + url + "?fid=" + flow.getId() + "&pid=" + pid + "&deptcode=" + deptcode;
            flowsJson.append("{\"fid\":\""+flow.getId()+"\",\"state\":\""+flow.getState()+"\",\"num\":\""+flow.getForder()+"\",\"txt\":\""+flow.getNickname()+"\",\"url\":\""+href+"\"},");

        }

        flowsJson=flowsJson.replace(flowsJson.lastIndexOf(","),flowsJson.length(),"");
        flowsJson.append("]");

        return  flowsJson;
    }

    public StringBuilder createZZFlowJson(List<ProjectInfoFlow> flowList,String projectType,HttpServletRequest request){
        StringBuilder flowsJson=new StringBuilder();
        flowsJson.append("[");
        for (ProjectInfoFlow flow : flowList) {
            int ftype = flow.getFid();
            String flowname=flow.getFname();
            if(!StringTools.trimNotEmpty(flowname)){
                flowname=flow.getNickname();
            }
            if(flowname.equalsIgnoreCase("政审")){
                flowname="背景调查";
            }
            String url = null;

            if (flowname.equalsIgnoreCase("毕业生推荐")) {
                url = projectType+"Recommend/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            }else if (flowname.equalsIgnoreCase("简历筛选")) {
                url = projectType+"Recommend/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("初审")) {
                url = projectType+"Review/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("投简历")) {
                url = projectType+"Resume/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("面试")) {
                url = projectType+"Interview/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("背景调查")) {
                url = projectType+"PReview/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("体检")) {
                url = projectType+"Physical/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("岗位分配")) {
                url = projectType+"Post/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("实习")) {
                url = projectType+"Practice/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("录用")) {
                url = projectType+"Employ/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else if (flowname.equalsIgnoreCase("笔试")) {
                url = projectType+"Exam/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            }else if (flowname.equalsIgnoreCase("签约")) {
                url = projectType+"Signing/?pid=" + flow.getPid() + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
            } else {
                url = "";
            }
            String href=request.getContextPath() + "/Do/" + url;
            flowsJson.append("{\"fid\":\""+flow.getId()+"\",\"state\":\""+flow.getState()+"\",\"num\":\""+flow.getForder()+"\",\"txt\":\""+flow.getNickname()+"\",\"url\":\""+href+"\"},");

        }
        flowsJson=flowsJson.replace(flowsJson.lastIndexOf(","),flowsJson.length(),"");
        flowsJson.append("]");
        return  flowsJson;

    }




}
